package com.sinodata.bsm.cicp.collector.weblogic8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:04:36   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class WebAppCollector extends Collector {

    private static final Logger logger = Logger.getLogger(WebAppCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        String port = this.getParamMap().get("Port").trim();//端口
        String username = this.getParamMap().get("Username").trim();//用户名
        String password = this.getParamMap().get("Password").trim();//密码
        String serverName = this.getParamMap().get("ServiceName").trim();//服务名称
        String webName = this.getParamMap().get("Name").trim();//名称
        String xmlString = XmlGetter.getJmxXml("WlgWebAppInfo", ip, port, username, password);
        // parse xml
        Document document;
        try {
            document = DocumentHelper.parseText(xmlString);
            Element rootElement = document.getRootElement();
            Iterator<?> iterator = rootElement.elementIterator("WebAppInfo");
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element != null) {
                    String svrName = element.element("ServerName").getTextTrim();
                    String nameString = element.element("Name").getTextTrim();
                    if (!svrName.equals(serverName)) {
                        continue;
                    } else if (nameString.equals(webName)) {
                        ResPropertyValue propertyValueX = new ResPropertyValue();
                        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                        //String name = element.element("Name").getTextTrim();
                        String status = element.element("Status").getTextTrim();
                        String contextRoot = element.element("ContextRoot").getTextTrim();
                        String sourceInfo = element.element("SourceInfo").getTextTrim();
                        String openSessionsCurrentCount = element.element("OpenSessionsCurrentCount").getTextTrim();
                        String openSessionsHighCount = element.element("OpenSessionsHighCount").getTextTrim();
                        String sessionsOpenedTotalCount = element.element("SessionsOpenedTotalCount").getTextTrim();
                        String servletCount = element.element("ServletCount").getTextTrim();
                        propValues[0] = ResPropertyValue.createPropValue(status, propMetas[0]);
                        propValues[1] = ResPropertyValue.createPropValue(contextRoot, propMetas[1]);
                        propValues[2] = ResPropertyValue.createPropValue(sourceInfo, propMetas[2]);
                        propValues[3] = ResPropertyValue.createPropValue(openSessionsCurrentCount, propMetas[3]);
                        propValues[4] = ResPropertyValue.createPropValue(openSessionsHighCount, propMetas[4]);
                        propValues[5] = ResPropertyValue.createPropValue(sessionsOpenedTotalCount, propMetas[5]);
                        propValues[6] = ResPropertyValue.createPropValue(servletCount, propMetas[6]);
                        /*
                         * values.add(new String[] { status, contextRoot,
                         * sourceInfo, "" + openSessionsCurrentCount, "" +
                         * openSessionsHighCount, "" + sessionsOpenedTotalCount, "" +
                         * servletCount });
                         */
                        propertyValueX.setValues(propValues);
                        values.add(propertyValueX);
                        return values;
                    }
                }
            }
            return null;
        } catch (DocumentException e) {
            logger.error("failed to collect by WebAppCollector", e);
            throw new CollectorException(e);
        }
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "用户名", "密码", "名称", "服务名称" };
        return new String[] { "Port", "Username", "Password", "Name", "ServiceName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "状态", "上下文根路径", "源信息", "当前会话数", "最高会话数", "会话总数", "servlet数" };
        return new String[] { "Status", "ContextRootPath", "ResourceInformation", "CurrentSessionsNum", "MaxSessionsNum", "SessionSum", "ServletNum" };
    }

}
